<?php

namespace App\Http\Controllers\Admin;

use App\Models\Auth;
use App\Models\User;
use function foo\func;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AuthController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $name = "";
        if (!empty($_GET['name'])) {
            $name = $_GET['name'];
        }
        $data= (new Auth())->getAllList(1);
        return view('admin.auth.index', compact('data'))->with('name', $name);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        $data= (new Auth())->getAllList(1);
        return view('admin.auth.create',compact('data'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->except('_token','url');
        if (empty($data['auth_name'])){
            return ['code'=>400,'msg'=>'权限名不能为空!'];
        }
        $sta = Auth::create($data);
        if ($sta) {
           return ['code'=>200,'msg'=>'新增成功!'];
        }
        return ['code'=>400,'msg'=>'新增失败!'];
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Auth  $auth
     * @return \Illuminate\Http\Response
     */
    public function show(Auth $auth)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Auth  $auth
     * @return \Illuminate\Http\Response
     */
    public function edit(Auth $auth)
    {
        //
        $data= (new Auth())->getAllList(1);
        $res = Auth::find($auth->id)->toArray();
        return view('admin.auth.edit',compact('data','res'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Auth  $auth
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Auth $auth)
    {
        $data = $request->except('_token', 'url');
        $sta = Auth::find($auth->id)->update($data);
        if ($sta) {
            return ['code' => 200, 'msg' => '修改成功!'];
        }
        return ['code' => 400, 'msg' => '修改失败!'];
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Auth  $auth
     * @return \Illuminate\Http\Response
     */
    public function destroy(Auth $auth)
    {
        //
        //查看是否有子权限
        $child = Auth::where('pid',$auth->id)->get()->toArray();
        if (!empty($child)){
            return ['code' => 400, 'msg' => '请先删除子权限!'];
        }
        $sta = Auth::find($auth->id)->delete();
        if ($sta) {
            return ['code' => 200, 'msg' => '删除成功!'];
        }
        return ['code' => 400, 'msg' => '删除失败!'];
    }
}
